// welcome.js
// 这里只能用相对路径，不能用绝对路径
var postsData = require('../../../data/post-data.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 小程序总是会读取data对象来做数据绑定，这个绑定是在onLoad事件执行之后发生的
    isPlayingMusic: false,
  },

  /**
   * 1.生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var globalData = app.globalData
    var postId = options.id;
    this.setData({
      currentId: postId
    })
    var tmpPostdata = postsData.postList[postId];
    console.log(tmpPostdata);
    var postsCollected = wx.getStorageSync("posts_collected");
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      this.setData({
        collected: postCollected
      })
    } else {
      var postCollected = {};
      postCollected[postId] = false;
      wx.setStorageSync("posts_collected", postCollected)
    }
    this.setData({
      postData: tmpPostdata
    })
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPistId === postId) {
      this.setData({
        isPlayingMusic: true
      })
    }
    this.setAudioMonitor();
    // 缓存最多不超过10Mb
    // 4种操作、8个方法，包括设置、得到、删除、清除，每个方法包括同步和异步两种
    // 同步
    //wx.setStorageSync("key", "车是数据")
    // 异步
    // wx.setStorage({
    //   key: 'key',
    //   data: {
    //     key: "mykey",
    //     value:"myvalue"
    //   },
    // })
    // wx.removeStorageSync("key");
    // // 清除所有缓存
    // wx.clearStorageSync();
  },
  setAudioMonitor: function () {
    var that = this;
    wx.onBackgroundAudioPlay(function () {
      that.setData({
        isPlayingMusic: true
      })
      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_currentMusicPistId = that.data.currentId;
    })
    wx.onBackgroundAudioPause(function () {
      that.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPistId = null;
    })
    wx.onBackgroundAudioStop(function () {
      that.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPistId = null;
    })
  },
  /**
   * 3.生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("onReady")
  },

  /**
   * 2.生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onCollectionTap: function (event) {
    var postsCollected = wx.getStorageSync("posts_collected");
    var postCollected = postsCollected[this.data.currentId];
    postCollected = !postCollected;
    postsCollected[this.data.currentId] = postCollected;
    this.showModal(postsCollected, postCollected);
  },
  showModal: function (postsCollected, postCollected) {
    var that = this;
    wx.showModal({
      title: "收藏",
      content: postCollected ? "收藏该文章" : "取消收藏该文章",
      showCanel: "true",
      cancelText: "取消",
      cancelColor: "#333",
      confirmText: "确认",
      confirmColor: "#405f80",
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync("posts_collected", postsCollected)
          that.setData({
            collected: postCollected
          })
          that.showToast(postCollected);
        }
      }
    })
  },
  showToast: function (postCollected) {
    // 更新文章是否收藏的缓存值
    wx.showToast({
      title: postCollected ? '收藏成功！' : '取消成功！',
      duration: 500,
      icon: "success"
    })
  },
  onShareTap: function (event) {
    var itemList = [
      "分享到微信好友",
      "朋友圈",
      "QQ好友",
      "分享空间"
    ]
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success: function (res) {
        //res.cancel 用户是否点击了取消
        //res.tapIndex 数组元素的序号，从0开始
        wx.showModal({
          title: '用户' + itemList[res.tapIndex],
          content: '暂时不支持分享功能',
        })
      }
    })
  },
  // 音乐播放
  onMusicTap: function () {
    var isPlayingMusic = this.data.isPlayingMusic;
    var currentPostId = this.data.currentId;
    var postData = postsData.postList[currentPostId];
    // 其中dataUrl和converImageUrl必须要用网络资源，不能用本地的
    if (!isPlayingMusic) {
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title: postData.music.title,
        converImageUrl: postData.music.coverImg
      })
    } else {
      
      wx.pauseBackgroundAudio();
    }
    this.setData({
      isPlayingMusic: !isPlayingMusic
    })
  }
})